package com.lily.aidouzixun.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

/**
 * 硬件的一些配置
 */
public class HardWare {
    private static final String TAG = "HardWare";

    /**
     * 屏幕大小
     */
    private static int screenWidth = 0;
    private static int screenHeight = 0;
    private static int densityDpi = 0;

    /**
     * 检查是否存在SDcard
     * @return
     */
    public static boolean hasSdcard() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 关闭软键盘
     * @param context
     * @param view
     */
    public static void closeKeyBoard(Context context, View view) {
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 得到屏幕大小、分辨率
     */
    private static void getScreenSize(Context appCon) {
        DisplayMetrics metrice = new DisplayMetrics();
        WindowManager manager = (WindowManager) appCon.getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getMetrics(metrice);
        screenWidth = metrice.widthPixels;
        screenHeight = metrice.heightPixels;
        densityDpi = metrice.densityDpi;
    }

    /**
     * 得到屏幕宽度
     * @param appCon
     * @return
     */
    public static int getScreenWidth(Context appCon){
        if (screenWidth == 0 && appCon != null) {
            getScreenSize(appCon);
        }
        return screenWidth;
    }

    /**
     * 设置屏幕宽度
     * @param width
     */
    public static void setScreenWidth(int width) {
        screenWidth = width;
    }

    /**
     * 得到屏幕高度
     * @param appCon
     * @return
     */
    public static int getScreenHeight(Context appCon) {
        if (screenHeight == 0 && appCon != null) {
            getScreenSize(appCon);
        }
        return screenHeight;
    }

    /**
     * 设置屏幕高度
     * @param height
     */
    public static void setScreenHeight(int height) {
        screenHeight = height;
    }
    /**
     * 得到屏幕分辨率
     * @param appCon
     * @return
     */
    public static int getDensityDpi(Context appCon) {
        if (densityDpi == 0 && appCon != null) {
            getScreenSize(appCon);
        }
        return densityDpi;
    }

    /**
     * 得到缩放比例
     * @param screenWidthRatio
     * @param HeightWidthRatio Height/Width
     * @return
     */
    public static int[] getScale(double screenWidthRatio, double HeightWidthRatio) {
        int width = (int) (screenWidth * screenWidthRatio);
        int height = (int) (width * HeightWidthRatio);
        return new int[] { width, height };
    }

    /**
     * 设置视图布局参数
     * @param view
     * @param screenWidthRatio 屏幕宽度
     * @param HeightWidthRatio 宽高比
     *                         eg:HardWare.setViewLayoutParams(img_head, 150 / 640.0, 1.0);
     */
    public static void setViewLayoutParams(View view, double screenWidthRatio, double HeightWidthRatio) {
        if(view != null) {
            int[] scale = getScale(screenWidthRatio, HeightWidthRatio);
            view.getLayoutParams().width = scale[0];
            view.getLayoutParams().height = scale[1];
        }
    }

    /**
     * 判断当前是否有网络
     * @param appContext
     * @return
     */
    public static boolean isNetworkAvailable(Context appContext) {
        ConnectivityManager connectivityManager = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        } else {
            NetworkInfo[] networkInfos = connectivityManager.getAllNetworkInfo();
            if (networkInfos != null && networkInfos.length > 0) {
                for (int i =0; i < networkInfos.length; i++ ) {
                    if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 解除资源
     * 一般在onDestroy里调用
     * @param view
     */
    public static void unbindDrawables(View view) {
        if (view == null) {
            return;
        }
        if (view.getBackground() != null) {
            view.getBackground().setCallback(null);
        }

        if (view instanceof ViewGroup) {
            int childCount = ((ViewGroup)view).getChildCount();
            for (int i = 0; i < childCount; i++) {
                unbindDrawables(((ViewGroup)view).getChildAt(i));
            }
            try {
                ((ViewGroup) view).removeAllViews();
            } catch (Exception e) {

            }
        }
    }

    /**
     * 从本地获取照片
     * @param filePath
     * @param reqW
     * @return
     */
    public static Bitmap loadImageFromFilePath(String filePath, int reqW) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        //inJustDecodeBounds设置为true, 不进行真正获取图片
        options.inJustDecodeBounds = true;
        //进行图片取样，只获取宽高等参数，不进行真正取样
        BitmapFactory.decodeFile(filePath, options);
        if (reqW < options.outWidth) {
            //根据option、所需宽高，算出缩放比例
            options.inSampleSize = (int) Math.round(options.outWidth * 1.0/ reqW);
        }
        //inJustDecodeBounds为false，真正获取图片
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);

    }

}
